﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Convertor.Interface;

namespace Convertor.Plugin.SaveToDisk
{
    /// <summary>
    /// ConfigForm.xaml 的交互逻辑
    /// </summary>
    public partial class ConfigForm : PluginConfigForm
    {

        public ConfigForm()
        {
            InitializeComponent();
        }

        private void UserControl_Loaded_1(object sender, RoutedEventArgs e)
        {
            bindEncoding();
            //ConfigContent config = Config as ConfigContent ?? new ConfigContent();
            //tbFilePath.Text = config.SaveFilePath;
            //cbEncodings.SelectedItem = config.FileEncoding;
        }

        private void bindEncoding()
        {
            string[] encoding = new string[] { "a" };
            var b = new Encoding[] { Encoding.Default };
            var a = b.Union(Encoding.GetEncodings().Select(p => p.GetEncoding())).OrderBy(p => p.EncodingName);
            cbEncodings.ItemsSource = a;
        }

        private void Button_Select_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Forms.FolderBrowserDialog fbd = new System.Windows.Forms.FolderBrowserDialog();
            if (this.tbFilePath.Text != string.Empty)
            {
                if (Directory.Exists(this.tbFilePath.Text))
                    fbd.SelectedPath = this.tbFilePath.Text;
            }
            fbd.ShowDialog();
            if (fbd.SelectedPath != string.Empty)
                ((ConfigContent)Config).SaveFilePath = fbd.SelectedPath;
        }

        private void Button_Clear_Click(object sender, RoutedEventArgs e)
        {
            config.SaveFilePath = null;
        }

        #region IPluginConfig 成员

        private ConfigContent config;
        public override object Config
        {
            get { return config ?? new ConfigContent(); }
            set { config = (value as ConfigContent) == null ? new ConfigContent() : (ConfigContent)((ICloneable)value).Clone(); }
        }

        #endregion

        #region IConfigFormEvent 成员

        public override object GetConfig()
        {
            return ((ConfigContent)Config).Clone();
        }

        #endregion

        #region IPluginHistory 成员

        public override object Input { get; protected set; }

        #endregion

    }
}
